import {webpackSay} from "../library"; //引入 library 的 webpackSay 方法
import Vue from "vue"; //导入vue
import noticeWebpack from "$NOTICE"; //导入noticeWebpack组件

document.write("fall in love with webpack！");
//按需引入girl.js文件
import("./girl").then((result) => {
    result.default();
    webpackSay(); //执行 webpackSay方法
    //告诉 webpack 想要带她去旅游--start---
    function renderNotice() {
        const noticeRootEle = document.createElement("div");
        document.body.append(noticeRootEle);
        new Vue({
            render: (h) => h(noticeWebpack)
        }).$mount(noticeRootEle);
    }
    renderNotice();
    //告诉 webpack 想要带她去旅游--end---
    //向webpack告白
    expressLove("webpack")
        .then((msg) => {
            writeToPage(`我说：${msg}`);
        })
        .catch((e)=>{
            writeToPage("我说：梁j茹，快来给我点勇气呀！！！");
        })
});

/**
 * 向webpack表白
 * @param name 参数
 */
function expressLove(name) {
    return new Promise((resolve, reject) => {
        const request = new XMLHttpRequest();
        request.onreadystatechange = () => {
            if (request.readyState === 4 && request.status === 200) {
                const result = JSON.parse(request.responseText);
                resolve(result.msg);
            }
        };
        request.onerror=(error)=>{
            reject(error);
        }
        request.open('GET', `/responseByName?name=${name}`);
        request.setRequestHeader("accept","application/json");
        request.send(null);
    });
}

/**
 * 打印msg到页面
 * @param msg
 */
function writeToPage(msg) {
    const ele = document.createElement("div");
    ele.innerText = msg;
    document.body.append(ele);
}